<?php
/**
 * 作为处理C类的补充辅助作用
 * Class PM_c_aux
 */
class PM_c_aux
{

    /**
     * 查询一个某个用户打出一个牌后；其他三人是否能操作
     * @info:即使能胡牌也不外乎是跑，下跑，吃，碰 - 所以只要有人能满足任意一条件；本次就拒绝延迟
     * @param $put_uid      出牌人
     * @param $card_number  出牌号
     * @param $db_connect   数据标识
     */
    public static function __queryHandPut_ISoperation( $room_info , $card_info ,$put_uid , $card_number , $db_connect ){
        //是否有人能胡？
        $is_hu = self::__is_hu( $room_info , $card_info , $put_uid , $card_number );
        if( $is_hu == true ){return true;}
        //跑
        $is_pao = self::__is_pao( $card_info , $put_uid , $card_number  );
        if( $is_pao == true ){return true;}
        //下跑
        $is_xia_pao = self::__is_xiapao(  $card_info , $put_uid ,  $card_number );
        if( $is_xia_pao == true ){return true;}
        //是否有人能碰
        $is_peng    = self::__is_peng( $room_info , $card_info , $put_uid ,  $card_number , $db_connect );
        if( $is_peng == true ){return true;}
        //下家是否能吃
        $is_xj_chi     = self::__xiajia_is_chi( $room_info , $card_info , $put_uid , $card_number , $db_connect );
        if( $is_xj_chi == true ){return true;}
        //都没有操作？
        return false;
    }

    //摸牌查询是否有人能操作(有点点不同，吃需要判断自己是否能吃；增加倒数第二个参数：摸牌人的手牌详情)
    /**
     * @param $room_info
     * @param $card_info
     * @param $mop_uid
     * @param $card_number
     * @param $mop_card_info
     * @param $db_connect
     * @return bool
     */
    public static function __queryHandPut_ISoperation_isMOP($room_info , $card_info , $mop_uid , $card_number , $mop_card_info , $db_connect ){
        //跑
        $is_pao = self::__is_pao( $card_info , $mop_uid , $card_number  );
        if( $is_pao == true ){return true;}
        //下跑
        $is_xia_pao = self::__is_xiapao(  $card_info , $mop_uid ,  $card_number );
        if( $is_xia_pao == true ){return true;}
        //是否有人能碰
        $is_peng    = self::__is_peng( $room_info , $card_info , $mop_uid ,  $card_number , $db_connect );
        if( $is_peng == true ){return true;}
        //自己是否能胡？
        $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_info['room_id'] , $mop_uid );
        $canHu        = PHZ_CanHu::__isCan_Hu_Pai( $default_huxi, $mop_card_info['sp'], $card_number , $card_number , $room_info );
        if ($canHu['canHu'] == true) {
            return true;
        }
        //自己是否能吃？
        if( PHZ_User::__Eat_card_is_Out( $room_info , $mop_uid , $card_number , $db_connect ) == true ){
            $chi_pai   = PHZ_Chi_Bi_Pai::__chi_pai_bi_pai_chk( $card_number , $mop_card_info['sp'] , $room_info );
            if( $chi_pai != false && count($chi_pai) >= 1 ){
                return true;
            }
        }
        //下家是否能吃
        $is_xj_chi     = self::__xiajia_is_chi( $room_info , $card_info , $mop_uid , $card_number , $db_connect );
        if( $is_xj_chi == true ){return true;}
        //都没有操作？
        return false;
    }

    //是否有人能胡，这里就判断普通的胡。因为跑胡有后面的跑的检测
    public static function __is_hu( $room_info , $card_info , $put_uid , $card_number){
        foreach ( $card_info as $key => $value ){
            if( $value['uid'] != $put_uid ){
                $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_info['room_id'] , $value['uid'] );
                $canHu = PHZ_CanHu::__isCan_Hu_Pai($default_huxi, $value['sp'], $card_number , -1 , $room_info );
                if( $canHu['canHu'] == true ){
                    return true;
                }
            }
        }
        return false;
    }

    //是否有人能碰牌
    public static function __is_peng(  $room_info , $card_info , $put_uid , $card_number , $db_connect ){
        foreach( $card_info as $key => $value ){
            if( $value['uid'] != $put_uid ) {
                $have_count = 0;//默认拥有0张
                $temp_sp = $value['sp'];$temp_sp = explode(",", $temp_sp);
                foreach( $temp_sp as $key_1 => $value_1 ){
                    if( $value_1 == $card_number ){$have_count++;}
                }
                //存在用户拥有2张，即可以碰；但（BUG：查询是否过章了，过章了则不允许返回false了）
                if( $have_count >= 2 && PHZ_User::__Peng_card_is_Out( $room_info , $value['uid'] , $card_number , $db_connect ) == true ){
                    return true;break;
                }
            }
        }
        return false;
    }

    public static function __xiajia_is_chi( $room_info , $card_info , $put_uid , $card_number , $db_connect ){
        $next_uid = PHZ_User::__get_user_Next_userID( $room_info , $put_uid );//获取下家用户ID
        foreach ($card_info as $key => $value) {
            if ($value['uid'] == $next_uid) {
                $next_user_card_info = $value;break;//获取下家的牌面信息
            }
        }
        if( PHZ_User::__Eat_card_is_Out( $room_info , $next_uid , $card_number , $db_connect  ) == true ) {
            $chi_pai = PHZ_Chi_Bi_Pai::__chi_pai_bi_pai_chk( $card_number, $next_user_card_info['sp'] , $room_info );//查询吃牌&比牌信息
            if ($chi_pai != false && count($chi_pai) >= 1) {
                return true;
            }
        }
        return false;
    }

    public static function __is_xiapao( $card_info , $put_uid ,  $card_number ){
        foreach( $card_info as $key => $value ){
            if( $value['uid'] != $put_uid ) {
                //必须是畏的牌，才存在手打牌“下跑”
                $card_is_wei = false;
                //切割每个人的畏过的牌的记录；如果能找到匹配当前打出的牌的话；才能进行查找畏牌记录
                $temp_card_wei = $value['card_wei'];$temp_card_wei = explode(",",$temp_card_wei);
                foreach ( $temp_card_wei as $key_2 => $value_2 ){
                    if( $value_2 == $card_number ){ $card_is_wei = true;break; }
                }
                if( $card_is_wei == true ){
                    return true;break;
                }
            }
        }
        return false;
    }

    public static function __is_pao(  $card_info , $put_uid , $card_number ){
        foreach( $card_info as $key => $value ){
            if( $value['uid'] != $put_uid ) {
                $have_count = 0;//默认拥有0张
                $temp_sp = $value['sp'];$temp_sp = explode(",", $temp_sp);
                foreach( $temp_sp as $key_1 => $value_1 ){
                    if( $value_1 == $card_number ){$have_count++;}
                }
                if( $have_count >= 3 ){
                    return true;break;
                }
            }
        }
        return false;
    }


}